package com.usian.article.service.impl

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.usian.article.feign.BehaviorFeign
import com.usian.article.feign.UserFeign
import com.usian.article.mapper.ApArticleConfigMapper
import com.usian.article.mapper.ApArticleContentMapper
import com.usian.article.mapper.ApCollectionBehaviorMapper
import com.usian.article.mapper.AuthorMapper
import com.usian.model.article.dtos.ArticleInfoDto
import com.usian.model.article.pojos.ApArticleConfig
import com.usian.model.article.pojos.ApArticleContent
import com.usian.model.common.base.BaseServiceImpl
import com.usian.model.common.dtos.ResponseResult
import com.usian.utils.threadlocal.AppThreadLocalUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import com.usian.model.common.enums.AppHttpCodeEnum
import com.usian.model.behavior.pojos.ApUnlikesBehavior
import com.usian.model.behavior.pojos.ApLikesBehavior
import com.usian.model.behavior.pojos.ApCollection
import com.baomidou.mybatisplus.core.toolkit.Wrappers
import org.apache.hadoop.hbase.Version.user


@Service
open class ArticleInfoServiceImpl : BaseServiceImpl<ApArticleContentMapper, ApArticleContent>() {
    @Autowired
    private lateinit var apArticleConfigMapper: ApArticleConfigMapper
    @Autowired
    private lateinit var userFeign: UserFeign

    @Autowired
    private lateinit var authorMapper: AuthorMapper

    @Autowired
    private lateinit var apCollectionMapper: ApCollectionBehaviorMapper

    @Autowired
    private lateinit var behaviorFeign: BehaviorFeign

    open fun loadArticleInfo(dto: ArticleInfoDto): ResponseResult<*> {
        if (dto === null || dto.articleId === null) return paramError
        var apArticleConfig = apArticleConfigMapper.selectOne(QueryWrapper<ApArticleConfig>().apply {
            eq("article_id", dto.articleId)
        }) ?: return paramDataExist
        val content = if (!apArticleConfig.isDelete && apArticleConfig.isDown) baseMapper.selectOne(QueryWrapper<ApArticleContent>().apply {
            eq("article_id", dto.articleId)
        }) ?: return paramDataExist else null
        return ResponseResult.okResult(mutableMapOf<String, Any?>("content" to  content, "config" to apArticleConfig))
    }

    //此接口用来  阅读文章详情的时候屏幕下方的点赞等等
    open fun findArticleBehavior(dto: ArticleInfoDto): ResponseResult<*> {
        dto.run { if (articleId == null || authorId == null) return paramError }
        val currentLoginId = AppThreadLocalUtils.getUser()?.id
        val entry = behaviorFeign.findByUserIdOrEquipmentId(currentLoginId, dto.equipmentId?:0)
                ?: return ResponseResult.okResult("没有行为实体")
        val unLike = behaviorFeign.getUnLike(entry.id, dto.articleId).let {
            it != null && it.type == ApUnlikesBehavior.Type.UNLIKE.code.toInt()
        }
        val like = behaviorFeign.getLike(dto.articleId, entry.id, ApLikesBehavior.Type.ARTICLE.code.toInt()).let {
            it != null && it.operation == ApLikesBehavior.Operation.LIKE.code
        }
        val collect = apCollectionMapper.selectOne(Wrappers.query<ApCollection>().eq("entry_id", entry.id).
                eq("article_id", dto.articleId).eq("type", ApLikesBehavior.Type.ARTICLE.code)).let {
            it != null
        }
        val follow = authorMapper.selectById(dto.authorId).run {
            userFeign.getFollow(currentLoginId, userId) !== null
        }
        return ResponseResult.okResult(mutableMapOf("isfollow" to follow, "islike" to like, "isunlike" to unLike, "iscollection" to collect))
    }
}